package com.example.springdemo6.controller;

import com.example.springdemo6.Model.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/controller")
@RestController
public class Controller {

    //获取单个参数
    @RequestMapping("/m1")
    public String m1(String name) {
        return "name:" + name;
    }

    //获取多个参数
    @RequestMapping("/m2")
    public String m2(String name, Integer age) {
        return "name: " + name + ",age: "+ age;
    }

    //参数重命名
    @RequestMapping("/m3")
    public String m3(@RequestParam("username") String name) {
        return "username: " + name;
    }

    //传递对象
    @RequestMapping("/m4")
    public String m4(User user) {
        return user.toString();
    }

    //传递数组
    @RequestMapping("/m5")
    public String m5(String[] arrays){
        return Arrays.toString(arrays);
    }


    //传递集合
    @RequestMapping("/m6")
    public String m6(@RequestParam List<String> list) {
        return "size: " + list.size() + list.toString();
    }

    //传递json
    @RequestMapping("/m7")
    public String m7() throws JsonProcessingException {
        User user = new User();
        user.setName("李四");
        user.setAge(22);
        user.setSex("男");
        ObjectMapper mapper = new ObjectMapper();
        //java对象转json
        String jsonUser = mapper.writeValueAsString(user);
        System.out.println(jsonUser);
        //json 转 Java
        User user1 = mapper.readValue(jsonUser, User.class);
        System.out.println("user1" + user1.toString());
        return "转换成功";

    }

    //使用注解
    public String m8(@RequestBody User user) {
        return user.toString();
    }
    //设置session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session != null) {
            session.setAttribute("name", "zhangsan");
            return "session 设置成功";
        }
        return "session 为空";
    }

    //获取session
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session == null) return "session 为空";
        String name = (String)session.getAttribute("name");
        return "name: " + name;
    }
    //使用注解的方式
    public String getSession2(@SessionAttribute(value = "name", required = false) String name){
        if(name == null) return "Session 为空";
        return "name: " + name;
    }

    //获取Cookie
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies == null) return "Cookie 为空";
        StringBuilder stringBuilder = new StringBuilder();
        for(Cookie cookie : cookies) {
            stringBuilder.append(cookie + "\n");
        }
        return stringBuilder.toString();
    }

    //获取URL中的参数
    @RequestMapping("/m9/{name}/{age}")
    public String m9(@PathVariable String name, @PathVariable String age) {
        return "name: "+ name + ",age: " + age;
    }

    //上传文件
    @RequestMapping("/m10")
    public String m10(@RequestPart MultipartFile file) throws IOException {
        String name = file.getName();
        file.transferTo(new File("D:/gitee/" + name));
        return "接收到的文件名称为：" + name;
    }




}
